import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {path: '/', name: 'home', component: ()=>import('../views/Home.vue'),meta: { requiresAuth: false },
     children: [{path: 'index', name: 'index', component: ()=>import('../views/index/index.vue'),meta: { requiresAuth: false } },]},
    //  书架路由
    {path: '/BookList', name: 'BookList', component: () => import('../views/BookList/BookList.vue'),meta: { requiresAuth: true } },
      //武侠
    {path: '/WuXia', name: 'WuXia', component: () => import('../views/Wuxia/Wuxia.vue'),meta: { requiresAuth: false } },
      //都市
    {path: '/DouShi', name: 'DouShi', component: () => import('../views/Doushi/Doushi.vue'),meta: { requiresAuth: false } },
      //科幻
    {path: '/KeHuang', name: 'KeHuang', component: () => import('../views/Kehuang/Kehuang.vue'),meta: { requiresAuth: false } },
      //历史
    {path: '/LiShi', name: 'LiShi', component: () => import('../views/Lishi/Lishi.vue'),meta: { requiresAuth: false } },
    //  言情
    {path: '/YanQin', name: 'YanQin', component: () => import('../views/Yanqin/Yanqin.vue'),meta: { requiresAuth: false } },
    //  网游
    {path: '/WangYou', name: 'WangYou', component: () => import('../views/Wangyou/Wangyou.vue'),meta: { requiresAuth: false } },
      //玄幻
    {path: '/XuanHuan', name: 'Xuanhuan', component: () => import('../views/Xuanhuan/Xuanhuan.vue'),meta: { requiresAuth: false } },
      //完本
    {path: '/Complete', name: 'Complete', component: () => import('../views/Complete/Complete.vue'),meta: { requiresAuth: true } },
    //  作家端
    {path: '/write', name: 'write', component: () => import('../views/write/write.vue'),meta: { requiresAuth: true } },
      //新闻
    {path: '/news/detail/:id', name: 'NewsDetail', component:()=>import('../views/news/news.vue') , props: true,meta: { requiresAuth: false } },// 允许将路由参数作为props传递给组件
      //搜索路由
    { path: '/search-results', name: 'SearchResults', component: () => import('../views/index/search-results.vue'),meta: { requiresAuth: false } }, // 添加搜索结果页路由
    //登录路由
    {path: '/login', name: 'login', component: () => import('@/views/login/WelcomeView.vue'), meta: { requiresAuth: false },
      children: [{path: '', name: 'welcome-login', component: () => import('@/views/login/Login.vue')},
        {path: '/register', name: 'welcome-register', component: () => import('@/views/login/RegisterPage.vue')},
        {path: '/reset', name: 'welcome-reset', component: () => import('@/views/login/ResetPage.vue')}]},
    // 添加管理员路由
    {path: '/admin', name: 'admin', component: () => import('@/views/admin/Admin.vue'), meta: {requiresAuth: true, requiresAdmin: true }, redirect: '/admin/users',
    children: [{path: 'users', name: 'admin-users', component: () => import('@/views/admin/UserManagement.vue')},
    {path: 'books', name: 'admin-books', component: () => import('@/views/admin/BookManagement.vue')},
    {path: 'news', name: 'admin-news', component: () => import('@/views/admin/NewsManagement.vue')}]},
   //书籍列表
    {path: '/books/list/:id', name: 'bookList', component: () => import('@/views/list/list.vue'), props: true,meta: { requiresAuth: false } },
      //章节路由
    {path: '/chapter/:bookId/:id', name: 'chapter', component: () => import('@/views/chapter/chapter.vue'), props: true,meta: { requiresAuth: false } },
    // 添加用户中心路由
    {path: '/user/profile', name: 'userProfile', component: () => import('@/views/user/Profile.vue'), meta: { requiresAuth: true } // 需要登录
    }
  ]
})
// src/router/index.js 末尾添加路由守卫
import { isLoggedIn, isAdmin } from '@/utils/authStorage.js';
import { ElMessage } from 'element-plus';

router.beforeEach((to, from, next) => {
  // 不需要登录的页面直接放行
  if (to.meta.requiresAuth === false) {
    next();
    return;
  }

  // 判断是否登录
  if (!isLoggedIn()) {
    ElMessage.warning('请先登录');
    next('/login');
    return;
  }

  // 需要管理员权限的页面
  if (to.meta.requiresAdmin) {
    if (isAdmin()) {
      next();
    } else {
      next('/');
    }
    return;
  }
  // 其他需要登录的页面
  next();
});
export default router